package com.llkj.contractmanagement.contractmanagement.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.llkj.contractmanagement.http.Constant;
import com.llkj.contractmanagement.main.BaseActivity;
import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.contractmanagement.actor.ActorDetailsActivity;
import com.llkj.contractmanagement.contractmanagement.adapter.MyCollectionActorAdapter;
import com.llkj.contractmanagement.customview.TitleBar;
import com.llkj.contractmanagement.http.HttpMethod;
import com.llkj.contractmanagement.http.UrlConfig;
import com.llkj.contractmanagement.main.MyApplication;
import com.llkj.contractmanagement.model.ActorBean;
import com.llkj.contractmanagement.model.UserInfoBean;
import com.llkj.contractmanagement.utils.GsonUtil;
import com.llkj.contractmanagement.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * author: dong.yj
 * created on: 2016/8/19 12:29
 * description:我的收藏界面
 */
public class MyCollectionActivity extends BaseActivity implements AdapterView.OnItemClickListener, BGARefreshLayout.BGARefreshLayoutDelegate {
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_mycollection;
    }

    @Bind(R.id.bga_refresh)
    BGARefreshLayout bgaRefreshLayout;
    @Bind(R.id.title_collection)
    TitleBar titleBar;
    @Bind(R.id.lv_collection)
    ListView lv_conllection;

    private List<ActorBean.YrlistBean> collectionList;
    private MyCollectionActorAdapter adapter;
    private UserInfoBean userInfoBean;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        init();
    }

    private void init() {
        initData();
        setListener();
    }

    private void setListener() {
        bgaRefreshLayout.setDelegate(this);
        titleBar.setTopBarClickListener(this);
        lv_conllection.setOnItemClickListener(this);
    }

    private void initData() {
        bgaRefreshLayout.setRefreshViewHolder(new BGANormalRefreshViewHolder(MyApplication.getInstance(),false));
        userInfoBean = UserInfoBean.getInstance();
        collectionList = new ArrayList<>();
        adapter = new MyCollectionActorAdapter(this, collectionList);
        lv_conllection.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        collectionList.clear();
        //请求收藏列表接口
        yrCollectionList();
    }

    private void yrCollectionList() {
        HashMap<String, String> map = new HashMap<>();
        map.put("user_id",userInfoBean.getId());
        map.put("token", userInfoBean.getToken());
        HttpMethod.yrCollection(this, this, map, UrlConfig.HTTP_YRCOLLECTION);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, ActorDetailsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("YrList", collectionList.get(position));
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void rightClick() {
        super.rightClick();
        if (collectionList.size() == 0) {
            ToastUtil.makeShortText(this, "您没有收藏的艺人");
        } else {
            Intent intent = new Intent(this, EditMyCollectionActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public void onSuccessHttp(String responseInfo, int resultCode) {
        super.onSuccessHttp(responseInfo, resultCode);
        switch (resultCode) {
            case UrlConfig.HTTP_YRCOLLECTION:
                bgaRefreshLayout.endRefreshing();
                ActorBean resultBean = GsonUtil.GsonToBean(responseInfo, ActorBean.class);
                if (resultBean != null) {
                    List<ActorBean.YrlistBean> addList = resultBean.getYrlist();
                    if (addList != null && addList.size() > 0) {
                        collectionList.addAll(addList);
                    }
                    adapter.notifyDataSetChanged();
                }
                break;
        }
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        collectionList.clear();
        //请求收藏列表接口
        yrCollectionList();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        return false;
    }
}
